/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package library;

import java.sql.*;

 
 
public class InternalFrameLibrary extends javax.swing.JInternalFrame {

    /**
     * Creates new form InternalFrameLibrary
     */
    public InternalFrameLibrary() {
        initComponents();
        System.out.println("Hello");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("collegeaccountant?zeroDateTimeBehavior=convertToNullPU").createEntityManager();
        libraryQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT l FROM Library l");
        libraryList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : libraryQuery.getResultList();
        labelLibraryModule = new javax.swing.JLabel();
        tableLibraryStudent = new javax.swing.JScrollPane();
        tableLibrary = new javax.swing.JTable();
        labelLibraryBG = new javax.swing.JLabel();
        label_StudentID = new javax.swing.JLabel();
        label_Vendor = new javax.swing.JLabel();
        btn_Update = new javax.swing.JButton();
        txt_StudentID = new javax.swing.JTextField();
        txt_Vendor = new javax.swing.JTextField();

        getContentPane().setLayout(null);

        labelLibraryModule.setFont(new java.awt.Font("Courier New", 1, 24)); // NOI18N
        labelLibraryModule.setForeground(new java.awt.Color(255, 255, 255));
        labelLibraryModule.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelLibraryModule.setText("Library Module ");
        getContentPane().add(labelLibraryModule);
        labelLibraryModule.setBounds(590, 10, 300, 40);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, libraryList, tableLibrary);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${studentID}"));
        columnBinding.setColumnName("Student ID");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${librarianID}"));
        columnBinding.setColumnName("Librarian ID");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${vendor}"));
        columnBinding.setColumnName("Vendor");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${registrationFee}"));
        columnBinding.setColumnName("Registration Fee");
        columnBinding.setColumnClass(Float.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${bookMagazineOrdered}"));
        columnBinding.setColumnName("Book Magazine Ordered");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${costPerPrice}"));
        columnBinding.setColumnName("Cost Per Price");
        columnBinding.setColumnClass(Float.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${qty}"));
        columnBinding.setColumnName("Qty");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fine}"));
        columnBinding.setColumnName("Fine");
        columnBinding.setColumnClass(Float.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        tableLibrary.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableLibraryMouseClicked(evt);
            }
        });
        tableLibraryStudent.setViewportView(tableLibrary);

        getContentPane().add(tableLibraryStudent);
        tableLibraryStudent.setBounds(330, 60, 1170, 430);
        getContentPane().add(labelLibraryBG);
        labelLibraryBG.setBounds(0, 0, 1910, 1200);

        label_StudentID.setText("Student ID");
        getContentPane().add(label_StudentID);
        label_StudentID.setBounds(40, 80, 60, 20);

        label_Vendor.setText("Vendor");
        getContentPane().add(label_Vendor);
        label_Vendor.setBounds(40, 130, 34, 14);

        btn_Update.setText("UPDATE");
        getContentPane().add(btn_Update);
        btn_Update.setBounds(60, 190, 73, 23);
        getContentPane().add(txt_StudentID);
        txt_StudentID.setBounds(130, 80, 70, 20);
        getContentPane().add(txt_Vendor);
        txt_Vendor.setBounds(130, 130, 70, 20);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tableLibraryMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableLibraryMouseClicked
      ResultSet rs = null;
       
      PreparedStatement pst=null;
        try
       {
            String dbUrl = "jdbc:mysql://localhost/collegeaccountant";
            String dbClass = "com.mysql.jdbc.Driver";
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection(dbUrl,"root","mysql");
            System.err.println("Connected!!!!");
            int row = tableLibrary.getSelectedRow();
            String Table_Click = (tableLibrary.getModel().getValueAt(row, 0).toString());
            String sql = " select * from library where StudentID = '"+Table_Click+"'";
            pst = con.prepareStatement (sql);
	    rs=pst.executeQuery(sql);
            if(rs.next())
            {
               String add1 = rs.getString("StudentID");
               txt_StudentID.setText(add1);
               
              // String add2 = rs.getString("LibrarianID");
              // txt_LibID.setText(add2);
               
               String add3 = rs.getString("Vendor");
               txt_Vendor.setText(add3);
               System.err.println(" Connected!!!!"+add1+" second Value"+add3);
           }
                  
       }
       catch (Exception e)
       {
           System.err.println("Not Connected!!!!"+e.getMessage());
       }
    }//GEN-LAST:event_tableLibraryMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Update;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel labelLibraryBG;
    private javax.swing.JLabel labelLibraryModule;
    private javax.swing.JLabel label_StudentID;
    private javax.swing.JLabel label_Vendor;
    private java.util.List<library.Library> libraryList;
    private javax.persistence.Query libraryQuery;
    private javax.swing.JTable tableLibrary;
    private javax.swing.JScrollPane tableLibraryStudent;
    private javax.swing.JTextField txt_StudentID;
    private javax.swing.JTextField txt_Vendor;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
